package com.zwb.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwb.boot.common.R;
import com.zwb.boot.common.exception.CustomException;
import com.zwb.boot.entity.Category;
import com.zwb.boot.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 头发又黑又长
 * @Date 2022/7/22 10:42
 * @email zwb15083976291@163.com
 */
@Api("分类管理")
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @ApiOperation(value = "新增分类接口")
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        categoryService.save(category);
        return R.success("新增分类成功");
    }


    /**
     * 获取分类分页数据
     *
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "获取分类分页数据接口")
    @GetMapping("/page")
    public R<Page<Category>> page(@RequestParam("page") Integer page,
                        @RequestParam("pageSize") Integer pageSize) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        Page<Category> pageInfo = new Page<>(page, pageSize);
        categoryService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);

    }

    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除分类接口")
    @DeleteMapping
    public R<String> remove(@RequestParam("ids") Long id) {
        try {
            categoryService.remove(id);
        } catch (CustomException e) {
            return R.error(e.getMessage());
        }
        return R.success("删除分类成功");
    }

    /**
     * 修改分类
     *
     * @param category
     * @return
     */
    @ApiOperation(value = "修改分类接口")
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        categoryService.updateById(category);
        return R.success("修改分类成功");
    }

    /**
     * 查询分类数据
     *
     * @param category
     * @return
     */
    @ApiOperation(value = "查询分类数据接口")
    @GetMapping("/list")
    public R<List<Category>> list(Category category) { // 推荐使用 类的方式，这样有通用性
        // 条件构造器
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 根据 type 查询
        lambdaQueryWrapper.eq(!ObjectUtils.isEmpty(category.getType()),
                Category::getType, category.getType())
                .orderByAsc(Category::getSort) // 排序条件
                .orderByDesc(Category::getUpdateTime);
        return R.success(categoryService.list(lambdaQueryWrapper));
    }

}


